package me.khrystal.rxnews.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.squareup.leakcanary.RefWatcher;

import me.khrystal.rxnews.annotation.ActivityFragmentInject;
import me.khrystal.rxnews.app.App;

/**
 * @FileName: me.khrystal.rxnews.base.BaseFragment.java
 * @Fuction: Fragment基类
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-03-08 10:27
 * @UpdateUser:
 * @UpdateDate:
 */
public abstract class BaseFragment<T extends BasePresenter> extends Fragment
        implements BaseView,View.OnClickListener{

    //common Presenter
    protected T mPresenter;

    protected View fragmentRootView;

    protected int mContentViewId;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (null == fragmentRootView){
            if (getClass().isAnnotationPresent(ActivityFragmentInject.class)){
                ActivityFragmentInject annotation = getClass()
                        .getAnnotation(ActivityFragmentInject.class);
                mContentViewId = annotation.contentViewId();
            }else {
                throw new RuntimeException(
                        "Class must add annotations of ActionFragmentInject.class"
                );
            }
            fragmentRootView = inflater.inflate(mContentViewId,container,false);
            initView(fragmentRootView);
        }
        return fragmentRootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mPresenter!=null){
            mPresenter.onResume();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ViewGroup parent = (ViewGroup)fragmentRootView.getParent();
        if (null!=parent){
            parent.removeView(fragmentRootView);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter!=null){
            mPresenter.onDestory();
        }
        // watch memory when fragment destory
        RefWatcher refWatcher = App.getRefWatcher(getActivity());
        refWatcher.watch(this);
    }

    public BaseFragment(){}

    protected abstract void initView(View fragmentRootView);

    /**
     * When called changeFragment() to display this method will called
     */
    public void onChange(){

    }

    protected void showSnackbar(String msg){
        Snackbar.make(fragmentRootView,msg,Snackbar.LENGTH_SHORT).show();
    }

    protected void showSnackbar(int id){
        Snackbar.make(fragmentRootView,id,Snackbar.LENGTH_SHORT).show();
    }

    /**
     * BaseView method
     */

    @Override
    public void toast(String msg) {
        showSnackbar(msg);
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void onClick(View v) {

    }
}
